



**********************************************************************************
**********************************************************************************

cd "`c(pwd)'"   // Set working directory to wherever the user placed "replication_package"

* Define global paths relative to the main directory
global main_dir "`c(pwd)'"
global data_dir "$main_dir/data_analysis"
global rawdata_dir "$main_dir/data_raw"
global tables_dir "$main_dir/tables"

* Load data
cd "$rawdata_dir/"

* Boardex Other Association
use boardex_other, clear
gen role_brd=substr(Role,-10,.)
tab role_brd
gen board=1 if role_brd== "(Brd) (SD)"
keep if board==1
tab ConnCompanyOrgType
rename BoardID companyid
rename CompanyID connexionid 
rename DirectorID directorid
rename OverlapYearStart  startyear
rename OverlapYearEnd endyear
replace endyear="2021" if  endyear=="Curr" 
destring  startyear endyear, replace
keep companyid connexionid directorid  startyear endyear
gen y1989=1 if startyear<=1989 & endyear>=1989 
gen y1990=1 if startyear<=1990 & endyear>=1990 
gen y1991=1 if startyear<=1991 & endyear>=1991 
gen y1992=1 if startyear<=1992 & endyear>=1992 
gen y1993=1 if startyear<=1993 & endyear>=1993 
gen y1994=1 if startyear<=1994 & endyear>=1994 
gen y1995=1 if startyear<=1995 & endyear>=1995
gen y1996=1 if startyear<=1996 & endyear>=1996 
gen y1997=1 if startyear<=1997 & endyear>=1997 
gen y1998=1 if startyear<=1998 & endyear>=1998 
gen y1999=1 if startyear<=1999 & endyear>=1999 
gen y2000=1 if startyear<=2000 & endyear>=2000 
gen y2001=1 if startyear<=2001 & endyear>=2001 
gen y2002=1 if startyear<=2002 & endyear>=2002 
gen y2003=1 if startyear<=2003 & endyear>=2003 
gen y2004=1 if startyear<=2004 & endyear>=2004 
gen y2005=1 if startyear<=2005 & endyear>=2005 
gen y2006=1 if startyear<=2006 & endyear>=2006 
gen y2007=1 if startyear<=2007 & endyear>=2007 
gen y2008=1 if startyear<=2008 & endyear>=2008 
gen y2009=1 if startyear<=2009 & endyear>=2009 
gen y2010=1 if startyear<=2010 & endyear>=2010 
gen y2011=1 if startyear<=2011 & endyear>=2011 
gen y2012=1 if startyear<=2012 & endyear>=2012 
gen y2013=1 if startyear<=2013 & endyear>=2013 
gen y2014=1 if startyear<=2014 & endyear>=2014 
gen y2015=1 if startyear<=2015 & endyear>=2015 
gen y2016=1 if startyear<=2016 & endyear>=2016 
gen y2017=1 if startyear<=2017 & endyear>=2017 
gen y2018=1 if startyear<=2018 & endyear>=2018 
gen y2019=1 if startyear<=2019 & endyear>=2019 
gen y2020=1 if startyear<=2020 & endyear>=2020
collapse (sum)  y1989 y1990 y1991 y1992 y1993 y1994 y1995 y1996 y1997 y1998 y1999 y2000 y2001 y2002 y2003 y2004 y2005 y2006 y2007 y2008 y2009 y2010 y2011 y2012 y2013 y2014 y2015 y2016 y2017 y2018 y2019 y2020 , by(companyid connexionid)
replace y1989=connexionid if y1989>=1
replace y1990=connexionid if y1990>=1
replace y1991=connexionid if y1991>=1
replace y1992=connexionid if y1992>=1
replace y1993=connexionid if y1993>=1
replace y1994=connexionid if y1994>=1
replace y1995=connexionid if y1995>=1
replace y1996=connexionid if y1996>=1
replace y1997=connexionid if y1997>=1
replace y1998=connexionid if y1998>=1
replace y1999=connexionid if y1999>=1
replace y2000=connexionid if y2000>=1
replace y2001=connexionid if y2001>=1
replace y2002=connexionid if y2002>=1
replace y2003=connexionid if y2003>=1
replace y2004=connexionid if y2004>=1
replace y2005=connexionid if y2005>=1
replace y2006=connexionid if y2006>=1
replace y2007=connexionid if y2007>=1
replace y2008=connexionid if y2008>=1
replace y2009=connexionid if y2009>=1
replace y2010=connexionid if y2010>=1
replace y2011=connexionid if y2011>=1
replace y2012=connexionid if y2012>=1
replace y2013=connexionid if y2013>=1
replace y2014=connexionid if y2014>=1
replace y2015=connexionid if y2015>=1
replace y2016=connexionid if y2016>=1
replace y2017=connexionid if y2017>=1
replace y2018=connexionid if y2018>=1
replace y2019=connexionid if y2019>=1
sort companyid connexionid 
reshape long y, i(companyid connexionid)
rename _j year
drop connexionid
rename y connexionid
drop if connexionid==0
sort year connexionid companyid 
drop if companyid==0
joinby companyid year using boardex_cusip_gvkey, unmatched(master)
drop _merge
drop if gvkey==.
rename connexionid directorid
cd "$data_dir/"
save directorid_otherlink_onlyBRD, replace
joinby directorid using boardex_empl, unmatched(master)
keep directorid year  gvkey
drop if gvkey==.
sort gvkey year
drop if gvkey==gvkey[_n-1] & year==year[_n-1]
save directorid_otherlink_onlyBRD, replace

** YEAR 1989
use directorid_otherlink_onlyBRD, clear
keep if year==1989
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 5 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/5 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/5 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 4
* create a director-ID variable for each shared director
forvalues i=2/4 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/4 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=1989
order year gvkey1 gvkey2
save overlapping_directors_other_1989, replace

** YEAR 1990
use directorid_otherlink_onlyBRD, clear
keep if year==1990
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/5 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/5 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
sort pair
bys pair: gen num=_N
sum num
* max: 4
* create a director-ID variable for each shared director
forvalues i=2/4 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/4 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=1990
order year gvkey1 gvkey2
save overlapping_directors_other_1990, replace

** YEAR 1991
use directorid_otherlink_onlyBRD, clear
keep if year==1991
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 5 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/5 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/5 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 6
* create a director-ID variable for each shared director
forvalues i=2/6 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/6 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=1991
order year gvkey1 gvkey2
save overlapping_directors_other_1991, replace

** YEAR 1992
use directorid_otherlink_onlyBRD, clear
keep if year==1992
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 5 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/5 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/5 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 4
* create a director-ID variable for each shared director
forvalues i=2/6 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/6 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=1992
order year gvkey1 gvkey2
save overlapping_directors_other_1992, replace

** YEAR 1993
use directorid_otherlink_onlyBRD, clear
keep if year==1993
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 6 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/6 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/6 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 7
* create a director-ID variable for each shared director
forvalues i=2/7 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/7 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=1993
order year gvkey1 gvkey2
save overlapping_directors_other_1993, replace

** YEAR 1994
use directorid_otherlink_onlyBRD, clear
keep if year==1994
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 6 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/6 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/6 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 7
* create a director-ID variable for each shared director
forvalues i=2/7 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/7 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=1994
order year gvkey1 gvkey2
save overlapping_directors_other_1994, replace

** YEAR 1995
use directorid_otherlink_onlyBRD, clear
keep if year==1995
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 6 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/6 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/6 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 11
* create a director-ID variable for each shared director
forvalues i=2/11 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/11 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=1995
order year gvkey1 gvkey2
save overlapping_directors_other_1995, replace

** YEAR 1996
use directorid_otherlink_onlyBRD, clear
keep if year==1996
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 6 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/6 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/6 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 8
* create a director-ID variable for each shared director
forvalues i=2/8 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/8 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=1996
order year gvkey1 gvkey2
save overlapping_directors_other_1996, replace


** YEAR 1997
use directorid_otherlink_onlyBRD, clear
keep if year==1997
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 6 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/6 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/6 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 8
* create a director-ID variable for each shared director
forvalues i=2/8 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/8 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=1997
order year gvkey1 gvkey2
save overlapping_directors_other_1997, replace


** YEAR 1998
use directorid_otherlink_onlyBRD, clear
keep if year==1998
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 10 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/10 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/10 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 8
* create a director-ID variable for each shared director
forvalues i=2/8 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/8 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=1998
order year gvkey1 gvkey2
save overlapping_directors_other_1998, replace


** YEAR 1999
use directorid_otherlink_onlyBRD, clear
keep if year==1999
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 8 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/8 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/8 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 9
* create a director-ID variable for each shared director
forvalues i=2/9 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/9 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=1999
order year gvkey1 gvkey2
save overlapping_directors_other_1999, replace


** YEAR 2000
use directorid_otherlink_onlyBRD, clear
keep if year==2000
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 9 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/9 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/9 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 13
* create a director-ID variable for each shared director
forvalues i=2/13 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/13 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2000
order year gvkey1 gvkey2
save overlapping_directors_other_2000, replace


** YEAR 2001
use directorid_otherlink_onlyBRD, clear
keep if year==2001
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 8 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/8 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/8 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 9
* create a director-ID variable for each shared director
forvalues i=2/9 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/9 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2001
order year gvkey1 gvkey2
save overlapping_directors_other_2001, replace


** YEAR 2002
use directorid_otherlink_onlyBRD, clear
keep if year==2002
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 7 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/7 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/7 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 13
* create a director-ID variable for each shared director
forvalues i=2/13 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/13 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2002
order year gvkey1 gvkey2
save overlapping_directors_other_2002, replace


** YEAR 2003
use directorid_otherlink_onlyBRD, clear
keep if year==2003
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 9 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/9 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/9 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 13
* create a director-ID variable for each shared director
forvalues i=2/13 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/13 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2003
order year gvkey1 gvkey2
save overlapping_directors_other_2003, replace


** YEAR 2004
use directorid_otherlink_onlyBRD, clear
keep if year==2004
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 8 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/8 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/8 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 11
* create a director-ID variable for each shared director
forvalues i=2/11 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/11 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2004
order year gvkey1 gvkey2
save overlapping_directors_other_2004, replace


** YEAR 2005
use directorid_otherlink_onlyBRD, clear
keep if year==2005
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 38 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/38 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/38 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 12
* create a director-ID variable for each shared director
forvalues i=2/12 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/12 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2005
order year gvkey1 gvkey2
save overlapping_directors_other_2005, replace


** YEAR 2006
use directorid_otherlink_onlyBRD, clear
keep if year==2006
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 36 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/36 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/36 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 13
* create a director-ID variable for each shared director
forvalues i=2/13 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/13 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2006
order year gvkey1 gvkey2
save overlapping_directors_other_2006, replace


** YEAR 2007
use directorid_otherlink_onlyBRD, clear
keep if year==2007
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 45 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/45 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/45 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 11
* create a director-ID variable for each shared director
forvalues i=2/11 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/11 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2007
order year gvkey1 gvkey2
save overlapping_directors_other_2007, replace


** YEAR 2008
use directorid_otherlink_onlyBRD, clear
keep if year==2008
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 45 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/45 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/45 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 12
* create a director-ID variable for each shared director
forvalues i=2/12 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/12 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2008
order year gvkey1 gvkey2
save overlapping_directors_other_2008, replace



** YEAR 2009
use directorid_otherlink_onlyBRD, clear
keep if year==2009
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 21 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/21 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/21 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 13
* create a director-ID variable for each shared director
forvalues i=2/13 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/13 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2009
order year gvkey1 gvkey2
save overlapping_directors_other_2009, replace



** YEAR 2010
use directorid_otherlink_onlyBRD, clear
keep if year==2010
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 45 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/45 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/45 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 12
* create a director-ID variable for each shared director
forvalues i=2/12 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/12 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2010
order year gvkey1 gvkey2
save overlapping_directors_other_2010, replace




** YEAR 2011
use directorid_otherlink_onlyBRD, clear
keep if year==2011
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 44 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/44 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/44 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 11
* create a director-ID variable for each shared director
forvalues i=2/11 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/11 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2011
order year gvkey1 gvkey2
save overlapping_directors_other_2011, replace



** YEAR 2012
use directorid_otherlink_onlyBRD, clear
keep if year==2012
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 14 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/14 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/14 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 12
* create a director-ID variable for each shared director
forvalues i=2/12 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/12 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2012
order year gvkey1 gvkey2
save overlapping_directors_other_2012, replace




** YEAR 2013
use directorid_otherlink_onlyBRD, clear
keep if year==2013
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 35 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/35 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/35 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 13
* create a director-ID variable for each shared director
forvalues i=2/13 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/13 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2013
order year gvkey1 gvkey2
save overlapping_directors_other_2013, replace



** YEAR 2014
use directorid_otherlink_onlyBRD, clear
keep if year==2014
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 31 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/31 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/31 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 13
* create a director-ID variable for each shared director
forvalues i=2/13 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/13 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2014
order year gvkey1 gvkey2
save overlapping_directors_other_2014, replace



** YEAR 2015
use directorid_otherlink_onlyBRD, clear
keep if year==2015
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 30 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/30 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/30 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 12
* create a director-ID variable for each shared director
forvalues i=2/12 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/12 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2015
order year gvkey1 gvkey2
save overlapping_directors_other_2015, replace



** YEAR 2016
use directorid_otherlink_onlyBRD, clear
keep if year==2016
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 23 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/23 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/23 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 13
* create a director-ID variable for each shared director
forvalues i=2/13 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/13 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2016
order year gvkey1 gvkey2
save overlapping_directors_other_2016, replace



** YEAR 2017
use directorid_otherlink_onlyBRD, clear
keep if year==2017
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 22 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/22 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/22 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 13
* create a director-ID variable for each shared director
forvalues i=2/13 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/13 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2017
order year gvkey1 gvkey2
save overlapping_directors_other_2017, replace


** YEAR 2018
use directorid_otherlink_onlyBRD, clear
keep if year==2018
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 20 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/20 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/20 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 12
* create a director-ID variable for each shared director
forvalues i=2/12 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/12 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2018
order year gvkey1 gvkey2
save overlapping_directors_other_2018, replace

** YEAR 2019
use directorid_otherlink_onlyBRD, clear
keep if year==2019
keep  directorid gvkey 
order  directorid gvkey 
sort  directorid gvkey 
bys directorid: gen num=_n
sum num
* max of 6 linked firm by one individual director
by directorid: gen last = _n == _N
by directorid: egen size = max(num) 
gen b=(size*size)-size+1
expand b if last, generate(added)
sort  directorid gvkey added
keep directorid gvkey added size
replace gvkey=. if added==1

gsort directorid -added gvkey
forvalues i=1/6 {
    replace gvkey=gvkey[_n+size] if gvkey==.
  }
rename gvkey gvkey1
gen gvkey2=.

by directorid: gen num = _n
forvalues i=1/6 {
    by directorid: replace gvkey2=gvkey1[_n+`i'] if num>size*(`i'-1) & num<=size*(`i')
  }

gen comb=(exp(lnfactorial(size)))/(2*(exp(lnfactorial(size-2))))
drop if num>comb
drop if gvkey2==.

keep directorid gvkey1 gvkey2 size
order gvkey1 gvkey2 directorid size
sort gvkey1 gvkey2 
egen pair=group(gvkey1 gvkey2)
*number of directors that companies share: 
sort pair
bys pair: gen num=_N
sum num
* max: 9
* create a director-ID variable for each shared director
forvalues i=2/9 {
    gen directorid`i'=.
    replace directorid`i'= directorid[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename directorid directorid1

rename size size_odlink
forvalues i=2/9 {
    gen size_odlink`i'=.
    replace size_odlink`i'= size_odlink[_n+(`i'-1)] if pair==pair[_n+(`i'-1)]
  }
rename size_odlink size_odlink1
label var size_odlink1 "number of boards of directorid1"

sort gvkey1 gvkey2 pair
drop if pair==pair[_n-1]
drop pair num 
gen year=2019
order year gvkey1 gvkey2
save overlapping_directors_other_2019, replace


** ALL YEARS TOGETHER:

use overlapping_directors_other_1989, clear
append using overlapping_directors_other_1990
append using overlapping_directors_other_1991
append using overlapping_directors_other_1992
append using overlapping_directors_other_1993
append using overlapping_directors_other_1994
append using overlapping_directors_other_1995
append using overlapping_directors_other_1996
append using overlapping_directors_other_1997
append using overlapping_directors_other_1998
append using overlapping_directors_other_1999
append using overlapping_directors_other_2000
append using overlapping_directors_other_2001
append using overlapping_directors_other_2002
append using overlapping_directors_other_2003
append using overlapping_directors_other_2004
append using overlapping_directors_other_2005
append using overlapping_directors_other_2006
append using overlapping_directors_other_2007
append using overlapping_directors_other_2008
append using overlapping_directors_other_2009
append using overlapping_directors_other_2010
append using overlapping_directors_other_2011
append using overlapping_directors_other_2012
append using overlapping_directors_other_2013
append using overlapping_directors_other_2014
append using overlapping_directors_other_2015
append using overlapping_directors_other_2016
append using overlapping_directors_other_2017
append using overlapping_directors_other_2018
append using overlapping_directors_other_2019

save overlapping_directors_other, replace

* each pair of firms must appear twice:
use overlapping_directors_other, clear
rename gvkey1 temp
rename gvkey2 gvkey1
rename temp gvkey2
save overlapping_directors_other_transf, replace
append using overlapping_directors_other
save overlapping_directors_other_double, replace
